﻿using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;

namespace Songhay.Composition
{
    /// <summary>
    /// Static members for MEF Composition.
    /// </summary>
    public static partial class CompositionUtility
    {
        /// <summary>
        /// Gets the MEF aggregate catalog of exports.
        /// </summary>
        /// <param name="pathToAssembly">The path to assembly.</param>
        /// <returns>Returns <see cref="System.ComponentModel.Composition.Hosting.AggregateCatalog"/>.</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope")]
        public static AggregateCatalog GetAggregateCatalog(string pathToAssembly)
        {
            var aggregate = new AggregateCatalog();
            var assemblyCatalog = CompositionUtility.GetAssemblyCatalog(pathToAssembly);
            aggregate.Catalogs.Add(assemblyCatalog);
            return aggregate;
        }

        /// <summary>
        /// Gets the MEF type catalog of exports.
        /// </summary>
        /// <typeparam name="T">Strongly Typed</typeparam>
        /// <param name="part">The part.</param>
        public static TypeCatalog GetTypeCatalog<T>(T part)
        {
            if(part == null) return null;
            Type t = part.GetType();
            return new TypeCatalog(t);
        }

        /// <summary>
        /// Throws the <see cref="CompositionException"/>.
        /// </summary>
        /// <param name="message">The message.</param>
        public static void ThrowCompositionException(string message)
        {
            throw new CompositionException(message);
        }
    }
}
